package com.hns.phone.pushMsg.web;




import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysRoleEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.ReturnResult;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.SysAppPhoneSetInfoEntity;
import com.hns.frame.exception.ParameterFormatException;
import com.hns.phone.pushMsg.service.PhoneMsgService;
import com.hns.phone.pushMsg.vo.PhoneMsgParamsVo;
import com.hns.tool.pub.UtilString;

/**
 * app推送信息controller(包括设置，更改用户token，tag相关通知下发)
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/api/phoneMsg")
public class PhoneMsgController extends BaseController {

	@Autowired
	private PhoneMsgService phoneMsgService;
	
	/**
	 * 更新用户token
	 * @param params
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping(value="updateUserToken")
	@ResponseBody
	public Object updateUserToken(){
		SysUserEntity user = this.getPhoneCurrentUser();
		PhoneMsgParamsVo params = new PhoneMsgParamsVo();
		try {
			JSONObject JSONParams = this.getParameter_JSON();
			String tokenId = JSONParams.getString("tokenId");
			String phoneType = JSONParams.getString("phoneType");
			params.setTokenId(tokenId);
			params.setPhoneType(phoneType);
		} catch (ParameterFormatException e) {
			e.printStackTrace();
		}
		phoneMsgService.updateUserToken(params, user);
		return new ReturnResult("", "1", true);
	}
	
	/**
	 * @param params
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping(value="getTagByUser")
	@ResponseBody
	public Object getTagByUser(PhoneMsgParamsVo params){
		List<SysRoleEntity>  arrayList = (List<SysRoleEntity>) request.getSession().getAttribute("CURRENTUSERROLELIST");
//		Set<SysRoleEntity> set_role = this.getAppCurrentUser().getSet_role();
//		ArrayList<SysRoleEntity> arrayList = new ArrayList<SysRoleEntity>(set_role);
		ArrayList<String> roleIds = new ArrayList<String>();
		for(SysRoleEntity role : arrayList){
			roleIds.add(role.getId());
		}
//		List<PhoneMsgVo> list = phoneMsgService.getTagByUser(params);
		return this.getReturnResult(roleIds);
	}
	
	/**
	 * 根据用户id获取推送设置信息
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("/getPushMsgSetting")
	@ResponseBody
    public Object getPushMsgSetting(){
		SysUserEntity user = this.getPhoneCurrentUser();
		SysAppPhoneSetInfoEntity pushMsgSetting = phoneMsgService.getPushMsgSetting(user);
		return this.getReturnResult(pushMsgSetting);
    }
	
	/**
	 * 设置报警信息
	 * @param entity
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("/updatePushMsgSetting")
	@ResponseBody
    public Object updatePushMsgSetting(){
		SysAppPhoneSetInfoEntity entity = new SysAppPhoneSetInfoEntity();
		try {
			JSONObject JSONParams = this.getParameter_JSON();
			entity.setSetId(JSONParams.getString("setId"));
			entity.setCorpId(JSONParams.getString("corpId"));
			entity.setUserId(JSONParams.getString("userId"));
			entity.setWthrAllowPush(JSONParams.getString("wthrAllowPush"));
			entity.setPushTime(JSONParams.getString("pushTime"));
			entity.setWthrReceAlamNotice(JSONParams.getString("wthrReceAlamNotice"));
			entity.setWthrNotiBarAlamNotice(JSONParams.getString("wthrNotiBarAlamNotice"));
			entity.setSoundRmndr(JSONParams.getString("soundRmndr"));
			entity.setShakeRmndr(JSONParams.getString("shakeRmndr"));
		} catch (ParameterFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		entity.setSetId(UtilString.getUUID());
    	String msgType = phoneMsgService.updatePushMsgSetting(entity);
    	return this.getReturnResult(msgType);
    }
		
}
